package com.huluobo.a2203parkdemo.visitor

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.databinding.ActivityAddVisitorBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.time.temporal.ChronoUnit
import java.util.Calendar
import java.util.Date

@AndroidEntryPoint
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding, VisitorViewModel>() {
    private var startTime: String = ""
    private var endTime: String = ""
    private var time: String = ""
    private var carNum: String = ""
    private var desc: String = ""
    override fun initData() {
        viewModel.addVisitorSuccess.observe(this) {
            ToastUtils.showLong("添加来访成功")
            finish()
        }
    }

    //        DatePickerDialog(
//            this,
//            object : OnDateSetListener {
//                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
//                }
//
//            }, 0, 0, 0
//        ).show()
    override fun initView() {
        var tampTime1: Int = 0
        var tampTime2: Int = 0

        //开始时间图标点击事件
        binding.addVisitorStartTimeBtn.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : DatePickerDialog.OnDateSetListener {
                    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                        startTime = "$year-${month + 1}-$dayOfMonth"
                        binding.addVisitorStartTimeNowTv.text = startTime
                        tampTime1 = dayOfMonth
                    }

                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorEndTimeBtn.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : DatePickerDialog.OnDateSetListener {
                    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                        endTime = "$year-${month + 1}-$dayOfMonth"
                        binding.addVisitorEndTimeNowTv.text = endTime
                        tampTime2 = dayOfMonth

                        if (tampTime2 - tampTime1 > 0) {
                            binding.addVisitorTimeEt.text = "${tampTime2 - tampTime1}天"
                            time = "${tampTime2 - tampTime1}天"
                        } else {
                            ToastUtils.showLong("选择的时间不能少于1")
                        }
                    }

                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }



        binding.addVisitorCommitBtn.setOnClickListener {
            carNum = binding.addVisitorCarNumEt.text.toString()
            desc = binding.addVisitorDetailEt.text.toString()

            if (startTime.isNotEmpty() &&
                endTime.isNotEmpty() &&
                time.isNotEmpty() &&
                carNum.isNotEmpty() &&
                desc.isNotEmpty()
            ) {
                val map = mutableMapOf<String, String>()
                map[Const.PARAM_CAR_NUM] = carNum
                map[Const.PARAM_VISITOR_START_TIME] = startTime
                map[Const.PARAM_VISITOR_END_TIME] = endTime
                map[Const.PARAM_VISITOR_TIME] = time
                map[Const.PARAM_VISITOR_DESC] = desc

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.addVisitor(body)
            } else {
                ToastUtils.showLong("来访信息不能为空")
            }

        }
    }
}